/* Copyright (c) 2009 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.google.orkut.client.api.samples.greetings.client.selector;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FocusPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Widget;
import com.google.inject.Inject;

/**
 * An item in an of {@link ItemSelector}. It is rendered as a
 * icon with a text below it.
 *
 * @author Shishir Birmiwal
 */
public class Item extends Composite implements ClickHandler {
  private static ItemUiBinder uiBinder = GWT.create(ItemUiBinder.class);

  interface ItemUiBinder extends UiBinder<Widget, Item> {
  }

  public static interface ItemSelectedCallback {
    public void selected(Item item);
  }

  @UiField
  Label text;

  @UiField
  Image icon;

  @UiField
  FocusPanel focus;

  String searchKey;

  ItemSelectedCallback cb;

  @Inject
  public Item() {
    initWidget(uiBinder.createAndBindUi(this));
    focus.addClickHandler(this);
  }

  public void setIcon(String url) {
    icon.setUrl(url);
  }

  public void setText(String text) {
    this.text.setText(text);
  }

  public void setSearchKey(String key) {
    searchKey = key;
  }

  public void setCallback(ItemSelectedCallback cb) {
    this.cb = cb;
  }

  @Override
  public void onClick(ClickEvent event) {
    click();
  }

  public void click() {
    if (cb != null) {
      cb.selected(this);
    }
  }

  public void setStyles(String userItem, String userItemIcon, String userItemText) {
    this.setStyleName(userItem);
    this.icon.setStyleName(userItemIcon);
    this.text.setStyleName(userItemText);
  }
}
